<?php
  switch($_GET['ajax']){
    // Settings
    case 'settings_window': showSettingsWindow(); break;
    case 'save_table_settings': saveTableSettings(); break;
    // Chat
    case 'chat_window': showChatWindow(); break;
    case 'chat_show_messages': echo json_encode(showChatMessages($_GET['section'])); break;
    case 'chat_post_message': echo json_encode(postChatMessage()); break;
  }

  // Functions
  function showSettingsWindow(){
    if(!isset($_SESSION['user']['id'])){ header('HTTP/1.1 403 Forbidden'); exit(); }
    global $tpl;
    require_once 'db.php';
    $tpl->add('settings', $_SESSION['user']['settings']);
  //  echo '!!!!!!!!!!'.DB::Encode('d797529d7da2ff307a73da308ae6ae17','1785364246').'!!!!!!!!!!';
    $tpl->display('settings.php');     
  }

  function saveTableSettings(){
    if(!isset($_SESSION['user']['id'])){ header('HTTP/1.1 403 Forbidden'); exit(); }
    require_once 'db.php';
    $DB = new DB();
    $DB->execute('UPDATE `users_settings` SET
                  `items` = \''.$_POST['items'].'\',
                  `weak` = \''.$_POST['weak'].'\',
                  `sort_by` = \''.$_POST['sort_by'].'\'
                  WHERE `user` = \''.$_SESSION['user']['id'].'\'');
    $_SESSION['user']['settings'] = array('items'=>$_POST['items'],
                                          'weak'=>$_POST['weak'],
                                          'sort_by'=>$_POST['sort_by']);
  }


  function showChatWindow(){
    if(!isset($_SESSION['user']['id'])){ header('HTTP/1.1 403 Forbidden'); exit(); }
    global $tpl;
    require_once 'db.php';

    $DB = new DB();
    $sections = $DB->execute("SELECT `id`, `name` FROM `chat_sections`");

    $tpl->add('sections', $sections);
    $tpl->add('messages', json_encode(showChatMessages($sections[0]['id'])));
    $tpl->display('chat.php'); 
  }

  function showChatMessages($id){
    if(!isset($_SESSION['user']['id'])){ header('HTTP/1.1 403 Forbidden'); exit(); }
    require_once 'db.php';
    require_once 'getsocialdata.php';
    $DB = new DB();
    $messages = $DB->Execute('SELECT m.`id`, m.`date`, m.`text`, u.`oauth_provider`, u.`username`, u.`role` FROM `chat_messages` m
                             LEFT JOIN `users` u ON u.`id` = m.`user`
                             WHERE m.`section_id` = \''.$id.'\' ORDER BY m.`date` DESC');
    foreach ($messages as $key=>$message){
      $user = getSocialData($message['oauth_provider'], $message['username']);
      $messages[$key]['link'] = $user['link'];
      $messages[$key]['avatar'] = $user['avatar'];
      $messages[$key]['name'] = $user['name'];
      $messages[$key]['text'] = str_replace("\n", '<br>',stripslashes ($message['text']));
    }
    return $messages;
  }

  function postChatMessage(){
    if(!isset($_SESSION['user']['id'])){ header('HTTP/1.1 403 Forbidden'); exit(); }
    require_once 'db.php';
    require_once 'getsocialdata.php';
    $DB = new DB();
    $_POST['text'] = mysql_real_escape_string(trim(strip_tags($_POST['text'])));
    $DB->Execute('INSERT INTO chat_messages SET 
                  `user` = \''.$_SESSION['user']['id'].'\',
                  `section_id` = \''.$_GET['section'].'\',
                  `text` = \''.$_POST['text'].'\'');
    $last_id = $DB->Execute('SELECT LAST_INSERT_ID() as id');
    $message = $DB->Execute('SELECT `date`, `text` FROM `chat_messages` WHERE `id` = \''.$last_id[0]['id'].'\'');
    $user = getSocialData($_SESSION['user']['oauth_provider'], $_SESSION['user']['username']);
    return array(0=>array('id'=>$last_id[0]['id'],
                          'link'=>$user['link'],
                          'avatar'=>$user['avatar'],
                          'name'=>$user['name'],
                          'date'=>$message[0]['date'],
                          'text'=>str_replace("\n", '<br>',stripslashes ($message[0]['text']))));
  }
?>
